package com.ihg.hiex.admin.web.controller.session.preparation;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.ihg.hiex.admin.entity.security.Login;
import com.ihg.hiex.admin.service.session.SessionReportService;
import com.ihg.hiex.admin.service.session.SessionService;
import com.ihg.hiex.admin.web.resolver.Secure;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.after.SessionReport;
import com.ihg.hiex.entity.session.meeting.SessionMeeting;
import com.ihg.hiex.entity.session.meeting.Target;
import com.ihg.hiex.entity.session.participator.Type;
import com.ihg.hiex.entity.session.task.SessionTask;
import com.ihg.hiex.entity.user.Delegate;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/sessions/{sessionId}")
@MenuSetting("menu-sessions")
public class DelegatePreparationController {
    
    private static final Logger logger = LoggerFactory.getLogger(DelegatePreparationController.class);
    
    @Autowired
    private SessionService sessionService;
    
    @Autowired
    private SessionReportService sessionReportService;
    
    /**
     * 
     * @param login
     * @param sessionId
     * @param page
     * @return
     */
    @MenuSetting(value = "submenu-delegate-preparation")
    @RequestMapping(path = "/preparations/delegate-summary", method = RequestMethod.GET)
    public ModelAndView summary(@Secure Login login,
                                @PathVariable("sessionId") Long sessionId, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/delegate-preparation/delegate-preparation-summary");
        
        Session session = sessionService.load(sessionId);
        modelAndView.addObject("session", session);
        
        // list assessors
        List<Delegate> delegates = sessionService.listDelegate(session);
        modelAndView.addObject("delegates", delegates);
        
        // list assessor's session task
        Map<String, Map<String, SessionTask>> sessionTasks = sessionService.listSessionTaks(session, Type.DELEGATE);
        modelAndView.addObject("sessionTasks", sessionTasks);
        
        // list delegate session report
        Map<String, SessionReport> reportMap = sessionReportService.list(session);
        modelAndView.addObject("reportMap", reportMap);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param login
     * @param sessionId
     * @param page
     * @return
     */
    @MenuSetting(value = "submenu-delegate-preparation")
    @RequestMapping(path = "/delegate-virtual-hotel", method = RequestMethod.GET)
    public ModelAndView virtualHotel(@Secure Login login,@PathVariable("sessionId") Long sessionId, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/virtual-hotel/delegate/virtual-hotel-list");
        
        Session session = sessionService.load(sessionId);
        modelAndView.addObject("session", session);
        
        List<SessionTask> sessionTasks = sessionService.listSessionTask(session, "Delegate The Virtual Hotel", Type.DELEGATE);
        modelAndView.addObject("sessionTasks", sessionTasks);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param login
     * @param sessionId
     * @param page
     * @return
     */
    @MenuSetting(value = "submenu-delegate-preparation")
    @RequestMapping(path = "/career-path", method = RequestMethod.GET)
    public ModelAndView careerPath(@Secure Login login,@PathVariable("sessionId") Long sessionId, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/career-path/career-path-list");
        
        Session session = sessionService.load(sessionId);
        modelAndView.addObject("session", session);
        
        List<SessionTask> sessionTasks = sessionService.listSessionTask(session, "Career Path", Type.DELEGATE);
        modelAndView.addObject("sessionTasks", sessionTasks);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param login
     * @param sessionId
     * @param page
     * @return
     */
    @MenuSetting(value = "submenu-delegate-preparation")
    @RequestMapping(path = "/evaluation", method = RequestMethod.GET)
    public ModelAndView evaluation(@Secure Login login,@PathVariable("sessionId") Long sessionId, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/evaluation/evaluation-view");
        
        Session session = sessionService.load(sessionId);
        modelAndView.addObject("session", session);
        
        List<Delegate> delegates = sessionService.listDelegate(session);
        modelAndView.addObject("delegates", delegates);
        
        Map<Long, Long> tasks = sessionService.listDelegateFinishTask(session, "Evaluation", delegates);
        modelAndView.addObject("tasks", tasks);
        
        Map<String, SessionTask> sessionTasks = sessionService.listDelegateSessionTask(session, "Evaluation");
        modelAndView.addObject("sessionTasks", sessionTasks);
        
        Map<String, SessionReport> sessionReports = sessionReportService.list(session);
        modelAndView.addObject("sessionReports", sessionReports);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param login
     * @param sessionId
     * @param page
     * @return
     */
    @MenuSetting(value = "submenu-delegate-preparation")
    @RequestMapping(path = "/delegate-qa-call", method = RequestMethod.GET)
    public ModelAndView qaCall(@Secure Login login,@PathVariable("sessionId") Long sessionId, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/qa-call/delegate/qa-call-view");
        
        Session session = sessionService.load(sessionId);
        modelAndView.addObject("session", session);
        
        // list delegates
        List<Delegate> delegates = sessionService.listDelegate(session);
        modelAndView.addObject("delegates", delegates);
        
        // get Q&A call meeting
        SessionMeeting sessionMeeting = sessionService.loadSessionMeeting(session, "Q&A Call", Target.ADMIN_DELEGATES);
        modelAndView.addObject("sessionMeeting", sessionMeeting);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param sessionId
     * @param meetingId
     * @return
     */
    @RequestMapping(path = "/delegate-qa-call/{meetingId}/edit-date", method = RequestMethod.GET)
    public ModelAndView editDate(@PathVariable("sessionId") Long sessionId,
                                 @PathVariable("meetingId") Long meetingId) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/qa-call/delegate/preparation-edit-date");
        
        SessionMeeting sessionMeeting = sessionService.loadSessionMeeting(meetingId);
        modelAndView.addObject("sessionMeeting", sessionMeeting);
        
        modelAndView.addObject("sessionId", sessionId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param sessionId
     * @param sessionMeeting
     * @return
     */
    @RequestMapping(value = "/delegate-qa-call/{id}/edit-date" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody String onEditDate(@PathVariable("sessionId") Long sessionId,
                                           @Preloading("meetingId") SessionMeeting sessionMeeting) {
        
        HttpResult httpResult = new HttpResult();

        try {
            
            sessionService.editSessionMeeting(sessionMeeting);
            
            httpResult.success().message("Submit Successful");
        
        } catch(Exception e) {
            httpResult.fail("500").message("failed");
            logger.error("#onEditDate() - ",e);
        }
        
        return JsonUtils.toJson(httpResult);
        
    }

}
